package cn.iune.system.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

@Service
public class AppListener implements ApplicationListener<ContextRefreshedEvent> {
	
	private Logger log = LogManager.getLogger(this.getClass());
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		// TODO Auto-generated method stub
		log.info(event.getSource() + "happends on " +event.getTimestamp());
		if(event.getApplicationContext().getParent() == null)//root application context 没有parent，他就是老大.
		{  
		     //需要执行的逻辑代码，当spring容器初始化完成后就会执行该方法。  
		    System.out.println("\n\n\n\n\n______________\n\n\n加载了\n\n_________\n\n");
		    log.info("onApplicationEvent应用已经启动（访问数据库的Bean已经加载完成），可以再次进行访问");
		    ApplicationContext ctx = event.getApplicationContext();
			System.out.println("8ctx.getApplicationName()-->>"+ctx.getApplicationName());
			String displayName = ctx.getDisplayName();
			System.out.println("8ctx.getDisplayName()-->>"+displayName);
			System.out.println("8ctx.getId()-->>"+ctx.getId());
			System.out.println("8ctx.getBeanDefinitionCount()-->>"+ctx.getBeanDefinitionCount());
		    
		}  
		
		//或者下面这种方式
		/*
		if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext"))
		{
		    System.out.println("\n\n\n_________\n\n加载一次的 \n\n ________\n\n\n\n");
		}	
		*/
	}
	

}
